<?php
App::import('Model','Assets.Asset');
class EventAsset extends EventsAppModel {

	var $name = 'EventAsset';

	const PUBLISHED = 1;
	const UNPUBLISHED = 0;
	var $scopes = array(
			'published' => array('EventAsset.status'=>self::PUBLISHED)
	);
	var $fieldProperties = array(
			"name" => array(
					'type'=>"file",
					"label"=>"File",
					"group"=>"add"
					)
					);
					//The Associations below have been created with all possible keys, those that are not needed can be removed
					var $belongsTo = array(
			'User'=> array(
					'className' => 'Users.User',
					'foreignKey' => 'user_id',
					'dependent' => true,
					'conditions' => '',
					'fields' => '',
					'order' => '',
					'counterCache'=>true,
					'counterScope'=>array('EventAsset.status'=>self::PUBLISHED)
					),

			'Event'=>array(
					'className' => 'Events.Event',
					'foreignKey' => 'event_id',
					'dependent' => true,
					'conditions' => '',
					'fields' => '',
					'order' => '',
					'counterCache'=>true,
					'counterScope'=>array('EventAsset.status'=>self::PUBLISHED)
					)
					);
					var $actsAs = array(
					/*'Upload'=>array(
					 'name'=>'name',
					 'settings'=>array(
					 'name'=> array(
					 'dir'=>WWW_ROOT.'uploads'.DS.'event_assets'.DS,
					 'mediaTypes'=>array("Image"),
					 'empty'=>$empty
					 )
					 )
					 ),*/
			'Users.UserContent'=>array(
					'posterModels'=>array('Users.User'=>array('dependent'=>true),'Users.Guest')
					),
			'Containable'
			);

			function afterInitialize()
			{
				$isImageRequired = cr('EventConfig.event_image_required');
				if($isImageRequired)
				{
					$empty = false;
				}else
				{
					$empty = true;
				}
				$uploadConfig = array("name"=> array('dir'=>WWW_ROOT.'uploads'.DS.'event_assets'.DS,'mediaTypes'=>array("Image"),'empty'=>$empty));
				$this->Behaviors->attach('Cms.Upload',$uploadConfig);
				$this->additionalFields[] = "file_url";
				parent::afterInitialize();
			}

			function beforeSave()
			{
				$return = parent::beforeSave();
				if(!isset($this->data['EventAsset']['name']) && count($this->data['EventAsset']) > 2)
				{
					unset($this->data['EventAsset']);
				}
				return $return;
			}

			function unpublishAssociatedEntity($idsToUpdate = null,$foreignKey = null)
			{
				if(!empty($foreignKey))
				{
					$idsToUpdate = $this->find('list',array('fields'=>array('id'),'conditions'=>array($this->alias.'.'.$foreignKey=>$idsToUpdate)));
					$this->updateAll(array($this->alias.'.status'=>self::UNPUBLISHED),array($this->alias.'.id'=>$idsToUpdate));
				}
				$this->refershCounters($idsToUpdate);

				$belongsToAssociations = $this->belongsTo;
				if(!empty($belongsToAssociations))
				{
					foreach($belongsToAssociations as $parentModel=>$assoc)
					{
						$foreignKey = $assoc['foreignKey'];
						$keys[$foreignKey] = $this->field($foreignKey,array($this->alias.'.id'=>$idsToUpdate));
						$this->updateCounterCache($keys);
					}
				}

				$hasManyAssociations = $this->hasMany;
				if(!empty($hasManyAssociations))
				{
					foreach($hasManyAssociations as $childModel=>$config)
					{
						if(method_exists($this->{$childModel},'unpublishAssociatedEntity'))
						{
							$this->{$childModel}->unpublishAssociatedEntity($idsToUpdate,$config['foreignKey']);
						}
					}
				}
			}

			function file_url($asset)
			{
				if(isset($asset[$this->alias]['name']))
				{
					return '/uploads/event_assets/'.$asset[$this->alias]['name'];
				}
				return false;
			}

			function beforeChildrenSave($data,$EventModel)
			{
				$userId = null;
				if(isset($EventModel->saveAllData['Event']['user_id']))
				{
					$userId = $EventModel->saveAllData['Event']['user_id'];
				}else if(isset($EventModel->saveAllData['Event']['id']))
				{
					/*since we are not using ownerId
					 $ownerId = $EventModel->field("owner_id",array("id"=>$EventModel->saveAllData['Event']['id']));
					 if(!$ownerId)
					 {
					 $userId = $EventModel->field("user_id",array("id"=>$EventModel->saveAllData['Event']['id']));
					 }else
					 {
					 $userId = $ownerId;
					 }*/
					$userId = null;
				}
				foreach($data as $key => $attachment)
				{
					if(!isset($attachment['name']) || !$attachment['name']['name'])
					{
						unset($data[$key]);
					}else
					{
						$data[$key]['status'] = self::PUBLISHED;
						if($userId)
						{
							$data[$key]['user_id'] = $userId;
						}
					}
				}
				return $data;
			}


			function makeAsDefault($eventId,$eventAssetId)
			{
				$eventAssetId = $this->field($this->alias.".id",array($this->alias.".id"=>$eventAssetId,$this->alias.".event_id"=>$eventId));
				if($eventAssetId)
				{
					$this->updateAll(array('is_default'=>0),array('EventAsset.event_id'=>$eventId));
					$this->id = $eventAssetId;
					//$this->saveField('is_default',1);
					$this->updateAll(array('is_default'=>1),array('EventAsset.event_id'=>$eventId,'EventAsset.id'=>$eventAssetId));
					$this->Event->clearRecordCache($eventId,true);
					return true;
				}
				return false;
			}

}
?>